#include "dict.h"
#include "la.h"
#include <stdlib.h>

struct sDict
{
	ListAsoc dic;
	int tam;
};

Dict
dict_empty(void)
{
	Dict d;
	d = (Dict) malloc (sizeof(struct sDict));
	if (d!=NULL)
	{
		d->tam = 0;
		d->dic = la_empty();
	}

return d;
}

void
dict_add(Dict d, Word w, Def f)
{
	Key k;
	Data dt;
	k = key_fromString(w);
	dt = data_fromString(f);
	
	la_add(d->dic,k,dt);
	d->tam += 1;

}
                             
Bool
dict_exists(Dict d, Word w)
{
	Bool b;
	Key k;
	k = key_fromString(w);
	b = la_exists(d->dic,k);
	k = key_destroy(k);
return b;
}


Def
dict_search(Dict d, Word w)
{
	Def f;
	Key k;
	Data dt;
	if (dict_exists(d,w))
	{
		k = key_fromString(w);
		dt = la_search(d->dic,k);
		f= data_toString(dt);
		k = key_destroy(k);
		dt = data_destroy(dt);
	}
	else
		f = NULL;

return f;
}
	
	
void
dict_del(Dict d, Word w)
{
	Key k;
	k = key_fromString(w);
	if (dict_exists(d,w))
	{
		la_del(d->dic,k);
		d->tam -= 1;
	}
	k = key_destroy(k);
}

void
print_dict(Dict d)
{
	print_la(d->dic);
}

int
dict_length(Dict d)
{
	int n;
	n = la_length(d->dic);
return n;
}

Dict
dict_destroy(Dict d)
{
	if (d!=NULL)
	{
		d->dic = la_destroy(d->dic);
		free(d);
	}
	d = NULL;
return d;
}
